<?php
class UserCredentials extends Zend_Db_Table_Abstract {
	
	protected $_primary = "id";
	protected $_name = "user_credentials";
	
	protected $_referenceMap = array (
    'User' => array (
        'columns' => 'user_id', 
        'refTableClass' => 'User', 
        'refColumns' => 'id'
//        'onDelete' => self::CASCADE,
//        'onUpdate' => self::RESTRICT
    )
    );
    
    /**
     * Add or update user credential.
     * It's not found in db with user id and given auth provider, then add it as new
     * 
     * @param $data
     * @return true if new credential is added, false if credential is already there
     */
    public function addCredential($data) {
    	$userCredential = $this->findByUserIdAndProvider($data["user_id"], $data["auth_provider"], UserCredentialStatus::ACTIVE);
    	if (!isset($userCredential)) {
    		$this->insert($data);
    		return true;
    	}
    	return false;
    }
    
    /**
     * Find user credential by user id and provider, it should be unique.
     * 
     * @param $user_id
     * @param $authProvider
     * @param $status
     */
    public function findByUserIdAndProvider($user_id, $authProvider) {
    	$select = $this->select()->where("user_id=?", $user_id)
    	->where("auth_provider=?",$authProvider);
    	return $this->fetchRow($select);
    	
    }
}
?>